package org.javaboy.vhr.controller.system.basic;

import com.alibaba.druid.util.StringUtils;
import org.javaboy.vhr.model.Position;
import org.javaboy.vhr.service.PositionService;
import org.javaboy.vhr.responseBean.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统管理基础信息职位信息controller
 * 路径
 */
@RestController
@RequestMapping("system/basic/position")
public class PositionController {

    @Autowired
    PositionService positionService;

    /**
     * 查询职位信息
     * @return
     */
    @GetMapping("/")
    public ServerResponse getAllPosition(){
        List<Position> positions = positionService.getAllPosition();
        return ServerResponse.success("",positions);
    }

    /**
     * 添加职位信息
     * @param position
     * @return
     */
    @PostMapping("/")
    public ServerResponse addPosition(@RequestBody Position position){
        if(StringUtils.isEmpty(position.getName())){
            return ServerResponse.error(1000,"职位名称不能为空");
        }
        if (positionService.addPosition(position)==1){
            return ServerResponse.success("添加成功");
        }
        return ServerResponse.error(1000,"添加失败");
    }

    /**
     * 修改职位信息
     * @param position
     * @return
     */
    @PutMapping("/")
    public ServerResponse updatePosition(@RequestBody Position position){
        if(StringUtils.isEmpty(position.getName())){
            return ServerResponse.error(1000,"职位名称不能为空");
        }
        if (positionService.updatePosition(position)==1){
            return ServerResponse.success("修改成功");
        }
        return ServerResponse.error(1000,"修改失败");
    }

    /**
     * 删除职位信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ServerResponse deletePositionById(@PathVariable Integer id){

        if (positionService.deletePositionById(id)==1){
            return ServerResponse.success("删除成功");
        }
        return ServerResponse.error(1000,"删除失败");
    }

    /**
     * 批量删除职位信息
     * @param ids
     * @return
     */
    @RequestMapping("/")
    public ServerResponse deletePositionByIds(Integer[] ids){
        if(positionService.deletePositionByIds(ids)==ids.length){
            return ServerResponse.success("删除成功!");
        }
        return ServerResponse.error(1000,"删除成功");
    }
}
